/********************************************************************************
* writer: vico
* create date: 2018-11-18
* description:封装 微信登录
*********************************************************************************/

(function(window){
  "user strict";
  /**
   * 设置item的对象属性参数
   * @param  {Object} params wx登录需要得参数，包括apiKey，apiSecret
   * @param  {callback} callback  登录成功后回调，
   *  微信模块调用失败的已经在内部处理，不需要自行处理
   *  1.初始化  wxLogin；
   *  2.调用    wxLogin的_login（）方法
   */
  var wxLogin = function(params,callback){
    this._init(params,callback);
  }
  var wx;
  wxLogin.prototype = {
    _param:'',
    _callback:'',
    _init:function(params,callback){
      wx = api.require("wx");
      this._param = params;
      this._callback = callback;
      console.log(JSON.stringify(this._param))
    },
    _checkWx:function(pCallBack){
      var self = this;
      wx.isInstalled(function(ret, err) {
          if (ret.installed) {
              pCallBack();
          } else {
              api.toast({
                  msg: '未安装微信，请尝试其他登录方式',
                  duration: 2000,
                  location: 'bottom'
              });
          }
      });
    },
    _getLoginAuthority:function(pCallBack){
      var self = this;
      wx.auth({
          apiKey: self._param.apiKey
      }, function(ret, err) {
          if (ret.status) {
              if (ret.status == true) { //授权成功
                  var tCode = ret.code;
                  if (tCode) {
                      pCallBack(tCode)
                  }
              }
          } else {
              console.log("【auth】获取失败")
          }
      });
    },
    _fnGetAccessToken:function(pCode,pCallBack){
      var self = this;
      wx.getToken({
          apiKey: self._param.apiKey,
          apiSecret: self._param.apiSecret,
          code: pCode
      }, function(ret, err) {
          if (ret.status) {
              var tAccessToken = ret.accessToken;
              var tOpenId = ret.openId;
              var tDynamicToken = ret.dynamicToken; //刷新token的时候用
              // self._fnGetUserInfo(tAccessToken, tOpenId)；
              pCallBack(tAccessToken, tOpenId)
          } else {
              console.log("【getToken】获取失败")
          }
      });
    },
    _fnGetUserInfo:function(pAccessToken,pOpenId){
       var self = this;
       wx.getUserInfo({
           accessToken: pAccessToken,
           openId: pOpenId
       }, function(ret, err) {
           if (ret.status) {
               self._callback(ret);
           } else {
               console.log("【getUserInfo】获取失败");
           }
       });
    },
    _login:function(){
      var self = this;
      /**检查是否安装微信**/
      this._checkWx(function(){
         /**获取登录授权**/
         self._getLoginAuthority(function(pCode){
            /**获取获取accessToken**/
            self._fnGetAccessToken(pCode,function(pAccessToken,pOpenId){
              /**获取用户信息**/
              self._fnGetUserInfo(pAccessToken,pOpenId)
            })
         })
      })
    }
  }
  window.wxLogin = wxLogin;
})(window)
